package com.linzelin.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 君未洋
 * 德鲁伊连接池配置类
 */
@Configuration
public class DruidConfig {

    /**
     * 用于将DruidSource与我们在application.yaml中配置的连接池连接起来
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }


    //后台监控功能
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");

        //配置后台管理的账号密码
        HashMap<String, String> initParameters = new HashMap<>();
        //增加配置
        initParameters.put("loginUsername","admin");
        initParameters.put("loginPassword","123456");
        //是固定的配置,这个就是配置对应的后台页面的账号和密码
        //注意这里不能写错
        //不然就直接进入对应的页面了

        //initParameters.put("allow","");
        //这里是写什么人能访问这个后台页面,或是什么端口可访问这个后台页面
        //如果是空的就代表都可以访问


        //这个就是限制谁不能登录了, initParameters.put("linxs","192.168.11.123");

        bean.setInitParameters(initParameters);
        //初始化参数
        return bean;
    }

    //filter
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();

        bean.setFilter(new WebStatFilter());
        //配置过滤器

        Map<String,String> initParameters = new HashMap<>();

        //配置这些东西不去过滤
        initParameters.put("exclusions","*.js,*.css,/druid/*");
        //可以过滤的请求
        bean.setInitParameters(initParameters);

        return bean;
    }
}
